package com.hurys.jclass;

import java.io.*;
import org.gjt.jclasslib.structures.Constant;
import org.gjt.jclasslib.io.ClassFileWriter;
import org.gjt.jclasslib.structures.ClassFile;
import org.gjt.jclasslib.structures.InvalidByteCodeException;
import org.gjt.jclasslib.structures.constants.ConstantStringInfo;
import org.gjt.jclasslib.structures.constants.ConstantUtf8Info;


/**
 * @author: sansan.si
 * @vesion 1.0
 * @date: 2022-02-10 09:01
 * @description: 直接修改.class文件
 **/
public class Test {
    public static void main(String[] args) throws IOException, InvalidByteCodeException {
        // .class 文件绝对的路径
        String filePath = "C:\\Users\\HP\\Desktop\\ActiveMqPoolFactory.class";
        FileInputStream fis = new FileInputStream(filePath);

        DataInput di = new DataInputStream(fis);
        ClassFile cf = new ClassFile();
        cf.read(di);
        Constant[] infos = cf.getConstantPool();

        int count = infos.length;
        for (int i = 0; i < count; i++) {
            if (infos[i] != null) {
                System.out.print(i);
                System.out.print(" = ");
                System.out.print(infos[i].getVerbose());
                System.out.print(" = ");
                System.out.println("end");
                // 通过jclasslib 工具 清楚修改的索引位置是 21
                if(i == 113){
                    ConstantUtf8Info uInfo = (ConstantUtf8Info)infos[i];
                    //设置修改的字符串
                    uInfo.setString("failover:(tcp://172.29.134.8:61616,tcp://172.29.134.9:61616)?initialReconnectDelay=100&maxReconnectDelay=1000");
                    infos[i]=uInfo;
                }
            }
        }
        cf.setConstantPool(infos);
        fis.close();
        File f = new File(filePath);
        ClassFileWriter.writeToFile(f, cf);
    }
}
